function [Y, t, Fs] = read_eclipse_avg(fn, n)
if nargin < 2, n=1; end

s = xml2struct(fn);

W = s.EPxxWaveforms.Waveform;
if iscell(W)
   W = W{n};
end
% s.EPxxWaveforms.Waveform.Gain
% s.EPxxWaveforms.Waveform.PrestimulusSamples
W.Gain;
npts = str2double(W.NumberOfStoredSamples.Text);
prestimSamples = str2double(W.PrestimulusSamples.Text);

a = ValueToArray(W.Response.IPSI_A_Raw);
b = ValueToArray(W.Response.IPSI_B_Raw);
c = ValueToArray(W.Response.Contra_A_Raw);
d = ValueToArray(W.Response.Contra_B_Raw);


Y = a+b;
%Y = c;
Y = Y(1:npts);

%Fs = 30000;
Fs = str2num(s.EPxxWaveforms.Waveform.Attributes.SampleRate);

t = (prestimSamples - 30 + (0:length(Y)-1)) * 1000 / Fs;    % Shift for prestimsamples + 1ms (eclipse extra delay)


function A = ValueToArray(R)

V = R.Value;
A = NaN(size(V));

for k = 1:length(V)
   A(k) = str2double(V{k}.Text);
end

